﻿using Logics.Sys;
using Models.Sys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zo.Models;

namespace BSAppDOne.WebAdmin.Areas.Admin.Controllers.Sys
{
    public class RoleController : BaseController
    {
        public RoleLogic _logic = new RoleLogic();
        // GET: Admin/Role
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GetData()
        {
            var data = _logic.GetAllRoles();
            return Success(data);
        }
        public ActionResult Assign(int roleId) 
        {
            //获取已经有的权限
            if(roleId==1)
            {
                throw new ErrorBox("Super Admin, disable action");
            }
            var opList=_logic.GetOperationListById(roleId);
            OperationLogic _Logic = new OperationLogic();
            var tree = _Logic.GetOperationTree(opList);
            ViewBag.tree = tree;
            ViewBag.roleId = roleId;
            return View();     
        }

        public ActionResult AssignDo(int[] ids, int roleId)
        {
            if(ids==null||ids.Count()<=0)
            {
                return Error("No items selected");
            }
            if(roleId==1)
            {
                return Error("You do not have permission to operate Super Admin permissions");
            }
            //保存权限
            var result=_logic.SaveRoleOperation(ids, roleId);
            if(!result)
            {
                return Error("Operation failed");
            }

            return Success();
        }

        public ActionResult GetRoleInfo(int roleId)
        {
            var role=_logic.GetRoleInfoById(roleId);
            return Success(role);
        }
        public ActionResult Save(Sys_Role info)
        {
            var result=_logic.Save(info);
            if (result)
            {
                return Success();
            }
            return Error();
        }
        public ActionResult Delete(int roleId)
        {
            var result=_logic.Delete(roleId);
            if (result)
            {
                return Success();
            }
            return Error();
        }
    }
}